package com.ant.myagile

import grails.test.mixin.*
import grails.test.mixin.support.*

import org.junit.*

import com.ant.myagile.backlog.Issue
import com.ant.myagile.backlog.PointRemain
import com.ant.myagile.backlog.Sprint
import com.ant.myagile.backlog.UserStory
import com.ant.myagile.dashboard.Status

/**
 * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
 */
@TestFor(ChartService)
@Mock([Member, Team, Team_Member,Project,Team_Project,Sprint,UserStory,PointRemain,Role,MemberRole,Issue,Status])
class ChartServiceTests {

	@Before
    void setUp() {
		Member.metaClass.encodePassword = { -> }
		
		//Add new user and new team
		def newUser = new Member(username: "khang.le@axonactive.vn", password: '123213', position: "Junior Java Developer", mobile: "972 731 980")
		.save(failOnError: true)
		Date establishedDate = new Date()
		Team newTeam = new Team(teamName: "Ant", mailGroup: "ant@axonactive.vn", establishedDate: establishedDate)
		.save(failOnError: true)

		def newTeamNewUser = new Team_Member()

		newUser.addToTeamMembers(newTeamNewUser)
		newTeam.addToTeamMembers(newTeamNewUser)
		newTeamNewUser.position = EnumPosition.DEVELOPER
		newTeamNewUser.save(failOnError : true)
		// Project
		def xProject = new Project(projectName: "X Project", description: "X Project Description", owner: newUser).save(failOnError : true)
		def yProject = new Project(projectName: "Y Project", description: "Y Project Description", owner: newUser).save(failOnError : true)

		//Team project
		def xA=new Team_Project(project: xProject, team: newTeam, startDate: new Date().parse("yy-MM-dd", "2013-06-03"), endDate: new Date().parse("yy-MM-dd", "2013-06-30")).save(failOnError:true)

		//asign Team Project to Project
		xProject.teamProjects=[xA]
		
		//Assign Team Project to Team
		newTeam.teamProjects=[xA]
		
		xProject.save(failOnError:true)
		yProject.save(failOnError:true)
		newTeam.save(failOnError:true)
		
		def aS0 = new Sprint(sprintName:"ABcd Sprint 0", status: "Close", dateStart: new Date().parse("yy-MM-dd", "2013-06-03"), dateEnd: new Date().parse("yy-MM-dd", "2013-06-08"), team: newTeam).save(failOnError:true)
		
		def xU0 = new UserStory(name:"xProject UserStory 0", description:"xProject UserStory Description 0", value: 50, project: xProject).save(failOnError:true)
		def xU1 = new UserStory(name:"xProject UserStory 1", description:"xProject UserStory Description 1", value: 30, project: xProject).save(failOnError:true)
		
		def sp0toDo = new Status(name: "Progress",color:'#368EE0', position: 1, hidden: false, start: true, done: false, team: newTeam, sprint: aS0).save(failOnError: true)
		
		def as0xu0I0 = new Issue(type:"UserStory", subject:"xProject Issue 0", description:"xProject Issue 0 Description", status:sp0toDo, priority:10, estimate:"D5T3", remain:"D1T3", sprint: aS0, userStory: xU0).save(failOnError:true)
		def as0xu1I1 = new Issue(type:"Task", subject:"xProject Issue 1", description:"xProject Issue 1 Description", status:sp0toDo, priority:20, estimate:"D5T3", remain:"D0T0", sprint: aS0, userStory: xU1).save(failOnError:true)
		def as0xu1I2 = new Issue(type:"Task", subject:"xProject Issue 2", description:"xProject Issue 2 Description", status:sp0toDo, priority:10, estimate:"D5T3", remain:"D0T0", sprint: aS0, userStory: xU1).save(failOnError:true)
		def as0xu1I3 = new Issue(type:"Bug", subject:"xProject Issue 3", description:"xProject Issue 3 Description", status:sp0toDo, priority:40, estimate:"D5T3", remain:"D0T1", sprint: aS0, userStory: xU1).save(failOnError:true)
		def as0xu1I4 = new Issue(type:"Task", subject:"xProject Issue 4", description:"xProject Issue 4 Description", status:sp0toDo, priority:10, estimate:"D5T3", remain:"D0T2", sprint: aS0, userStory: xU1).save(failOnError:true)
		
		def as0xp0=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-03"),pointRemain: "D5T3", issue: as0xu0I0).save(failOnError:true)
		def as0xp1=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-03"),pointRemain: "D8T3", issue: as0xu1I1).save(failOnError:true)
		def as0xp2=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-03"),pointRemain: "D13T3", issue: as0xu1I2).save(failOnError:true)
		def as0xp3=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-03"),pointRemain: "D5T3", issue: as0xu1I4).save(failOnError:true)
		
		def as0xp4=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-04"),pointRemain: "D4T3", issue: as0xu0I0).save(failOnError:true)
		def as0xp5=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-04"),pointRemain: "D3T3", issue: as0xu1I1).save(failOnError:true)
		def as0xp6=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-04"),pointRemain: "D12T3", issue: as0xu1I2).save(failOnError:true)
		def as0xp7=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-04"),pointRemain: "D5T3", issue: as0xu1I4).save(failOnError:true)
		
		def as0xp8=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-05"),pointRemain: "D3T3", issue: as0xu0I0).save(failOnError:true)
		def as0xp9=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-05"),pointRemain: "D5T3", issue: as0xu1I1).save(failOnError:true)
		def as0xp10=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-05"),pointRemain: "D5T3", issue: as0xu1I2).save(failOnError:true)
		def as0xp11=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-05"),pointRemain: "D4T3", issue: as0xu1I4).save(failOnError:true)
		
		def as0xp12=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-06"),pointRemain: "D0T3", issue: as0xu0I0).save(failOnError:true)
		def as0xp13=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-06"),pointRemain: "D3T3", issue: as0xu1I1).save(failOnError:true)
		def as0xp14=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-06"),pointRemain: "D7T3", issue: as0xu1I2).save(failOnError:true)
		def as0xp15=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-06"),pointRemain: "D3T1", issue: as0xu1I3).save(failOnError:true) //a bug occured
		def as0xp16=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-06"),pointRemain: "D3T3", issue: as0xu1I4).save(failOnError:true)
		
		def as0xp17=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-07"),pointRemain: "D0T0", issue: as0xu0I0).save(failOnError:true) //issue done
		def as0xp18=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-07"),pointRemain: "D0T3", issue: as0xu1I1).save(failOnError:true)
		def as0xp19=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-07"),pointRemain: "D5T3", issue: as0xu1I2).save(failOnError:true)
		def as0xp20=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-07"),pointRemain: "D0T3", issue: as0xu1I3).save(failOnError:true)
		def as0xp21=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-07"),pointRemain: "D0T3", issue: as0xu1I4).save(failOnError:true)
		
		def as0xp23=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-08"),pointRemain: "D0T0", issue: as0xu1I1).save(failOnError:true)
		def as0xp24=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-08"),pointRemain: "D4T3", issue: as0xu1I2).save(failOnError:true)
		def as0xp25=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-08"),pointRemain: "D0T0", issue: as0xu1I3).save(failOnError:true)
		def as0xp26=new PointRemain(dateUpdate: new Date().parse("yy-MM-dd", "2013-06-08"),pointRemain: "D0T1", issue: as0xu1I4).save(failOnError:true)
		
		as0xu0I0.pointRemains=[as0xp0, as0xp4, as0xp8, as0xp12, as0xp17]
		as0xu1I1.pointRemains=[as0xp1, as0xp5, as0xp9, as0xp13, as0xp18, as0xp23]
		as0xu1I2.pointRemains=[as0xp2, as0xp6, as0xp10, as0xp14, as0xp19, as0xp24]
		as0xu1I3.pointRemains=[as0xp15, as0xp20, as0xp25]
		as0xu1I4.pointRemains=[as0xp3, as0xp7, as0xp11, as0xp16, as0xp21, as0xp26]
		
		aS0.issues = [as0xu0I0, as0xu1I1, as0xu1I2, as0xu1I3, as0xu1I4]
		
		aS0.save(failOnError:true)
    }

    void tearDown() {
        // Tear down logic here
    }

    void testSomething() {
		def dateList=[new Date().parse("yy-MM-dd", "2013-06-03"),
			       new Date().parse("yy-MM-dd", "2013-06-04"),
			       new Date().parse("yy-MM-dd", "2013-06-05"),
			       new Date().parse("yy-MM-dd", "2013-06-06"),
			       new Date().parse("yy-MM-dd", "2013-06-07"),
			       new Date().parse("yy-MM-dd", "2013-06-08")]
		
        ChartService c= new ChartService()
		def sprint1=c.getDataChartBySprint("1")
		assertEquals(sprint1.pList, [8+11+16+8, 7+6+15+8, 6+8+8+7, 3+6+10+4+6, 3+8+3+3, 7+1])
		assertEquals(sprint1.iList, [4, 4, 4, 5, 4, 2])
		assertEquals(sprint1.bList, [0, 0, 0, 4, 3, 0])
		assertEquals(sprint1.dList.toList(), dateList)
    }
}
